//
//  RTJAutoMarkAlertView.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/12.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJAutoMarkAlertView.h"
#import "LewPopupViewController.h"

@interface RTJAutoMarkAlertView ()
@property (strong, nonatomic) IBOutlet UIView *popview;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UIView *bg_view;
@property (weak, nonatomic) IBOutlet UITextField *textFld1;
@property (weak, nonatomic) IBOutlet UIView *bg_view2;

@property (weak, nonatomic) IBOutlet UITextField *textFld2;
@property (nonatomic,assign) BOOL isType;
@end



@implementation RTJAutoMarkAlertView


- (id)initWithFrame:(CGRect)frame type:(BOOL)type
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
        _popview.frame = frame;
        [self addSubview:_popview];
        
        _isType = type;
        
        _popview.layer.masksToBounds = YES;
        _popview.layer.cornerRadius = 9;
        
        _bg_view.layer.masksToBounds = YES;
        _bg_view.layer.cornerRadius = 5;
        _bg_view.layer.borderColor = [UIColor colorForHex:@"#CCCCCC"].CGColor;
        _bg_view.layer.borderWidth = 1;
        
        _bg_view2.layer.masksToBounds = YES;
        _bg_view2.layer.cornerRadius = 5;
        _bg_view2.layer.borderColor = [UIColor colorForHex:@"#CCCCCC"].CGColor;
        _bg_view2.layer.borderWidth = 1;

        
        _titleLab.text = type ? @"借款期限":@"利率范围";
        _lab1.text = type ? @"个月":@"%";
        _lab2.text = type ? @"个月":@"%";
        _lab3.text = type ? @"注:最低借款期限为1个，最高为12个月。":@"注:7%-18%为推荐利率范围！";
        
        
    }
    return self;
}

+ (instancetype)defaultPopupViewWithFrame:(CGRect)rect type:(BOOL)type{
    
    return [[RTJAutoMarkAlertView alloc]initWithFrame:rect type:type];
}


- (IBAction)cancel:(id)sender {
    
    [_textFld1 resignFirstResponder];
    [_textFld2 resignFirstResponder];
    
    [_parentVC lew_dismissPopupView];

}

- (IBAction)sure:(id)sender {
    
    [_textFld1 resignFirstResponder];
    [_textFld2 resignFirstResponder];
    
    if (_textFld1.text.length ==0||_textFld2.text.length ==0 ) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入利率范围"];
        return;
    }
    
    NSInteger min = [_textFld1.text integerValue];
    NSInteger max = [_textFld2.text integerValue];
    
    if (min<7 || min>18) {
        
        [SVProgressHUD showErrorWithStatus:@"7%-18%为有效利率范围"];
        return;
    }
    
    if (max<7 || max>18) {
        
        [SVProgressHUD showErrorWithStatus:@"7%-18%为有效利率范围"];
        return;
    }

    if (min>max) {
        
        [SVProgressHUD showErrorWithStatus:@"请正确输入利率范围格式，如7%-18%"];
        return;
    }
    
    if (self.sureClick) {
        
        self.sureClick(_textFld1.text,_textFld2.text);
    }
    
    [_parentVC lew_dismissPopupView];

}

-(void)withSureClick:(sureBtnBlock)block{
    
    _sureClick = block;
}
@end
